<template>
  <div class="register-bg">
    <div class="register-center">
      <div class="register-card">
        <div class="register-header">
          <h1>用户注册</h1>
          <p class="subtitle">创建您的账户</p>
        </div>
        <form @submit.prevent="handleRegister">
          <div class="input-group">
            <span class="input-icon"><i class="iconfont icon-user"></i></span>
            <input v-model="form.username" type="text" placeholder="请输入用户名" required class="input" />
          </div>
          <div class="input-group">
            <span class="input-icon"><i class="iconfont icon-user"></i></span>
            <input v-model="form.name" type="text" placeholder="请输入姓名" required class="input" />
          </div>
          <div class="input-group">
            <span class="input-icon"><i class="iconfont icon-email"></i></span>
            <input v-model="form.email" type="email" placeholder="请输入邮箱" required class="input" />
          </div>
          <div class="input-group">
            <span class="input-icon"><i class="iconfont icon-lock"></i></span>
            <input :type="showPwd ? 'text' : 'password'" v-model="form.password" placeholder="请输入6~12位密码" required class="input" />
            <span class="eye-icon" @click="showPwd = !showPwd">
              <i class="iconfont" :class="showPwd ? 'icon-eye' : 'icon-eye-close'"></i>
            </span>
          </div>
          <div class="input-group">
            <span class="input-icon"><i class="iconfont icon-lock"></i></span>
            <input :type="showConfirmPwd ? 'text' : 'password'" v-model="form.confirmPassword" placeholder="请确认密码" required class="input" />
            <span class="eye-icon" @click="showConfirmPwd = !showConfirmPwd">
              <i class="iconfont" :class="showConfirmPwd ? 'icon-eye' : 'icon-eye-close'"></i>
            </span>
          </div>
          <div class="row-between agree-row">
            <label class="checkbox-label">
              <input type="checkbox" v-model="agree" />
              同意《平台服务协议》和《隐私政策》
            </label>
          </div>
          <button type="submit" :disabled="loading || !agree || !isFormValid" class="register-btn">
            {{ loading ? '注册中...' : '注 册' }}
          </button>
          <p v-if="error" class="error">{{ error }}</p>
        </form>
        <div class="login-link">
          已有账户？<a href="#" @click.prevent="goToLogin">立即登录</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { userAPI } from '@/services/api';

export default {
  name: 'RegisterPage',
  data() {
    return {
      form: {
        username: '',
        name: '',
        email: '',
        password: '',
        confirmPassword: ''
      },
      showPwd: false,
      showConfirmPwd: false,
      agree: false,
      loading: false,
      error: ''
    };
  },
  computed: {
    isFormValid() {
      return this.form.username && 
             this.form.name && 
             this.form.email && 
             this.form.password && 
             this.form.confirmPassword &&
             this.form.password === this.form.confirmPassword &&
             this.form.password.length >= 6;
    }
  },
  methods: {
    async handleRegister() {
      if (!this.agree || !this.isFormValid) return;
      
      this.loading = true;
      this.error = '';
      
      try {
        const data = await userAPI.register({
          username: this.form.username,
          name: this.form.name,
          email: this.form.email,
          password: this.form.password
        });
        
        if (data.success) {
          // 注册成功，跳转到登录页
          this.$router.push('/login?registered=true');
        } else {
          this.error = data.error || '注册失败';
        }
      } catch (e) {
        this.error = e.message || '网络错误，请稍后重试';
      } finally {
        this.loading = false;
      }
    },
    goToLogin() {
      this.$router.push('/login');
    }
  }
};
</script>

<style scoped>
@import url('//at.alicdn.com/t/c/font_4244842_1v7v6k2k2k.css');

.register-bg {
  min-height: 100vh;
  width: 100vw;
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}

.register-center {
  width: 100vw;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.register-card {
  background: #fff;
  border-radius: 28px;
  padding: 56px 32px 36px 32px;
  width: 94vw;
  max-width: 430px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.register-header {
  text-align: center;
  margin-bottom: 38px;
}

.register-header h1 {
  font-size: 2.3rem;
  font-weight: 700;
  color: #222;
  margin-bottom: 14px;
}

.subtitle {
  color: #bbb;
  font-size: 1.1rem;
  margin-bottom: 0;
}

.input-group {
  display: flex;
  align-items: center;
  background: #f6f7fa;
  border-radius: 16px;
  margin-bottom: 28px;
  padding: 0 18px;
  border: 1px solid #f0f0f0;
  height: 54px;
}

.input-icon {
  color: #bbb;
  font-size: 22px;
  margin-right: 10px;
}

.input {
  flex: 1;
  border: none;
  background: transparent;
  font-size: 18px;
  padding: 14px 0;
  outline: none;
}

.eye-icon {
  color: #bbb;
  font-size: 20px;
  cursor: pointer;
  margin-left: 10px;
}

.row-between {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  margin-bottom: 18px;
}

.checkbox-label {
  font-size: 15px;
  color: #666;
  user-select: none;
}

.agree-row {
  margin-bottom: 28px;
}

.register-btn {
  width: 100%;
  padding: 16px 0;
  background: #9ee6b0;
  color: #fff;
  border: none;
  border-radius: 28px;
  font-size: 20px;
  font-weight: 600;
  cursor: pointer;
  margin-bottom: 18px;
  letter-spacing: 8px;
  transition: background 0.2s;
}

.register-btn:active {
  background: #7edc97;
}

.register-btn:disabled {
  background: #e0e0e0;
  color: #fff;
  cursor: not-allowed;
}

.error {
  color: #e74c3c;
  margin-top: 12px;
  font-size: 15px;
  text-align: center;
}

.login-link {
  margin-top: 28px;
  text-align: center;
  font-size: 16px;
  color: #666;
}

.login-link a {
  color: #4caf50;
  text-decoration: none;
  font-weight: 600;
}
</style> 